<template>
  <div class="other-funds">
    <div class="headline">
      <span></span>TA的开放式基金
    </div>

    <div class="list">
      <div class="entry clearfix" v-for="(item,index) in stockList">
        <div class="one">
          <div class="value">{{item.ofname}}</div>
          <div class="key">{{item.ofcode}}</div>
        </div>
        <div class="two">
          <div class="value">{{item.lastPrice}}</div>
          <div class="key">净值/元</div>
        </div>
        <div class="three">
          <div class="value">{{item.currentVol}}</div>
          <div class="key">份额</div>
        </div>
        <div class="four">
          <div class="value" :class="item.incomeBalance<0?'color-green':'color-red'">{{item.incomeBalance}}</div>
          <div class="key">累积收益</div>
        </div>
      </div>
    </div>
    <div class="placeholder" v-if="stockList.length==0">
      <div class="content">
        <div class="tips" v-if="error ==''">哎呦，TA还没有<span class="">基金持仓</span>哦</div>
        <div class="tips" v-if="error !=''">{{error}}</div>
      </div>
    </div>
  </div>
</template>

<script>
  import RemoteDAO from 'common/remoteDAO'
  import CommonUtil from 'common/common.util'
  import LocalDAO from 'common/localDAO'

  export default {
    data () {
      return {
        userCode: LocalDAO.getUserCode(),
        stockList: [],
        error: '',
        testData: 'testData'
      }
    },
    props: {
      clientId: {
        type: String
      }
    },
    created: function () {
      this.fetchData()
    },
    watch: {
      '$route.query.userCode' (to, from) {
        //跳转相同页面，组件不重新构建，无法调用created,故需监听
        this.fetchData()
      }
    },
    methods: {
      fetchData(){
        var self = this;
        var param = {
          start: 0,
          limit: 500,
          client_id: this.clientId
        }
        RemoteDAO.callJCL11171(param, function (response) {
          if (Number(response.error) === 0) {
            var list = [];
            _.each(response.data.body.holders, function (item, index) {
              var object = {};
              object.ofname = item.ofname;
              object.ofcode = item.ofcode;
              object.exchangeType = item.exchange_type;
              object.costPrice = CommonUtil.toFixed(item.keep_cost_price, 2); // 成本价
              object.lastPrice = CommonUtil.toFixed(item.last_price, 2); // 净值
              object.currentVol = CommonUtil.toFixed(item.current_vol, 2); // 份额
              object.incomeBalance = CommonUtil.toFixed(item.income_balance, 2); // 盈亏金额
              if (object.incomeBalance >= 0) {
                object.incomeBalance = "+" + object.incomeBalance;
              }
              list.push(object);
            });
            self.stockList = list;

          } else {
            self.error = response.data;
          }
        });
      },
      toStock(item){
//        var stockCode = item.stockCode;
//        RemoteDAO.callStockLive(stockCode, "ordinary", "1", function () {
//        });
      }
    }
  }
</script>

<style scoped>

</style>
